﻿/*
 * 文件名称：
 * 程序员：huangsb
 * 创建时间：2008/06/10
 * 
 * 功能说明：重命名[模板文件或文件夹]
 * 
 * 修改人员：
 * 
 * 修改日期：
 * 
 * 修改记录：
 */

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Diagnostics;
using Whir.Common.WebHelper;

public partial class Whir_System_Module_Template_ResetName : System.Web.UI.Page
{
    protected char isPostBack;    //是否第一次访问

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            this.isPostBack = '0';
        else
            this.isPostBack = '1';
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        string dir = this.hfDir.Value;
        string newname = this.txt1.Text;
        string path;

        dir = dir.Replace("%3a", ":");
        FileInfo fileInfo = new FileInfo(dir.Replace("%3a", ":"));
        path = dir.Substring(0, dir.LastIndexOf('\\') + 1);

        if ((fileInfo.Attributes & FileAttributes.Directory) != 0)
        {
            //文件夹
            if (RenameFolder(dir, path + newname))
                Response.Write("<script type='text/javascript'>window.returnValue=1;window.close();</script>");
            else
                Response.Write("<script type='text/javascript'>alert('该文件夹名已存在！');window.returnValue=1;window.close();</script>");
        }
        else
        {
            //文件
            if (RenameFile(dir, path + newname + Path.GetExtension(dir)))
                Response.Write("<script type='text/javascript'>window.returnValue=1;window.close();</script>");
            else
                Response.Write("<script type='text/javascript'>alert('该文件名已存在！');window.returnValue=1;window.close();</script>");
        }
    }

    /// <summary>
    /// 重命名文件夹
    /// </summary>
    /// <param name="oldPath">初始路径</param>
    /// <param name="newPath">新路径</param>
    private bool RenameFolder(string oldPath, string newPath)
    {
        try
        {
            System.IO.Directory.Move(oldPath, newPath);
            return true;
        }
        catch (IOException ex)
        {
            return false;
        }
    }

    /// <summary>
    /// 重命名文件
    /// </summary>
    /// <param name="oldPath">初始路径</param>
    /// <param name="newPath">新路径</param>
    private bool RenameFile(string oldPath, string newPath)
    {
        try
        {
            System.IO.File.Move(oldPath, newPath);
            return true;
        }
        catch (IOException ex)
        {
            return false;
        }
    }
}
